package sqlToGUI;


import sqlToGUI.charts.GUIDisplay;
import sqlToGUI.charts.LogPanel;
import sqlToGUI.charts.helperUtilities.TelemetryDataEntry;
import sqlToGUI.sqlRetrievalAlgorithms.Alg_Test;
import sqlToGUI.sqlRetrievalAlgorithms.SQLDataRetrievalAlgorithm;
import sqlToGUI.sqlRetrievalAlgorithms.array.Alg_ArrayPowerMap;
import sqlToGUI.sqlRetrievalAlgorithms.car.Alg_CarDistTravelled;


public class ChartController extends Thread{
	
	/**List of buffers for a chart, in case the chart updates from
	 * more than one buffer */
	private TelemetryDataEntry [] buffers;
	
	/**List of algorithms to be used to send the data to the chart
	 * in case there is more than one buffer for the chart */
	private SQLDataRetrievalAlgorithm [] algorithm;
	
	/**The frequency at which the chart will be updated to display
	 * new data */
	private int timer;
	
	/**Name of the data being displayed by this controller */
	private String display;
	/**The chart managed by this controller */
	private GUIDisplay chart;
	
	/**Class constructor */
	public ChartController(String display, GUIDisplay chart, int timer){
		this.display = display;
		this.chart = chart;
		this.buffers = chart.getBuffers();
		this.timer = timer;
		init();
	}
	
	private void init(){

		/***************************************************/
		/***************************************************/
		/***************************************************/
		/******Used With Currently Displayed Charts*********/
		/***************************************************/
		/***************************************************/
		/***************************************************/
		
		if(display.equals("MotorSpeedometer")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}

		else if(display.equals("WeatherWindDirection")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
	
		else if(display.equals("WeatherWindSpeedBar")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("ArrayCurrentInBar")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("BatteryCurrentInCurrentOutBar")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("MotorCurrentBar")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("ArrayCurrentMap")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("ArrayCurrentInChart")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("MotorCurrentInChart")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("WeatherWindSpeedChart")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}	
		
		else if(display.equals("MotorSpeedChart")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}	
		
		else if(display.equals("BatteryCapacityBar")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("CarDistanceMap")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_CarDistTravelled(buffers);
		}
	 
		/***************************************************/
		/***************************************************/
		/***************************************************/
		/***************************************************/
		/***************************************************/
		/***************************************************/

		
		else if(display.equals("MotorVoltage")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("MotorAcceleration")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("MotorTemperature")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("WeatherHumidity")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("WeatherTemperature")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("CarDirection")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("BatteryCurrentIn")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("BatteryCurrentOut")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}
		
		else if(display.equals("BatteryMinMax")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		}		

		else if(display.equals("ArrayVoltage")){
			algorithm = new SQLDataRetrievalAlgorithm [1];
			algorithm[0] = new Alg_Test(buffers);
		} 		

		else {
			algorithm = new SQLDataRetrievalAlgorithm [0];
		}
		
	}
	
	/**Register the log to the chart managed by this controller */
	public GUIDisplay registerLogToChart(LogPanel log){
		return chart.registerLog(log);
	}
	
	/**Method running on a thread for updating the chart */
	public void run(){
		while(true){
			try {
				Thread.sleep(timer);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for(int i=0; i<algorithm.length; i++){
				algorithm[i].updateData();
			}
		}
	}
	
}
